
import { routerRedux } from 'dva/router';
import api from '../api';

import moment from 'moment';
import { notification } from 'antd';

export default {
	namespace: 'pdtedit',
	state:{
		pdtlist:[]
	},
	subscriptions:{
		setup({dispatch,history}) {
			history.listen(({pathname,query}) => {
				if( pathname === '/systemMgt/accountMgt/pdtMgt/revise' ){
					dispatch({
						type: 'getonelist',
						payload: query
					})
				}
			})
		}
	},
	effects:{
		* getonelist({ payload:query }, { call, put }) {
			yield put({
				type:'initData',
				payload:{
					pdtlist:[]
				}
			})
			const response = yield call(api.getonelist,query.productId);
			yield put({
				type: 'getListSuccess',
				payload: response ,
			});
		},
		* savePdtEdit({ payload }, { call, put }) {
			const productId = payload.productId;
			const response = yield call(api.savePdtEdit,productId,{...payload});
			yield put({
				type: 'saveSuccess',
				payload: { response },
			});
		},
	},
	reducers:{
		initData(state, {payload}) {
			return {
				...state,
				...payload
			}
		},
		getListSuccess(state, {payload}) {
			const {data,page} = payload
			data.effectiveDate = moment(data.effectiveDate).format('YYYY-MM-DD');
			data.bondRate = data.bondRate * 100
			data.futuresRate = data.futuresRate * 100
			data.sharesRate = data.sharesRate * 100 
			data.isIncludeCash = `${data.isIncludeCash}`
			const status=data ? true:false
			// console.log('pdtelist>>',data)
			return {
				...state,
				status,
				pdtlist:data,
			}
		},
		saveSuccess(state, action) {
			const { payload: { response } } = action;
			notification['success']({
		   message: '保存成功',
		  });
			return {
				...state,
			};
		},
	}
}